package com.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dao.AsSystemConfigDao;
import com.entity.AsSystemConfig;
import com.utils.JsonUtil;
import com.utils.Pager;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public class AsSystemConfigService
{
    @Resource
    private AsSystemConfigDao asSystemConfigDao;

    //返回指定类型的json格式
    public String returnAvalibleTypeJsonStr( Integer configType )
    {
        ArrayList<AsSystemConfig> typeList = asSystemConfigDao.getAvalibleType( configType );
        String jsonStr = JsonUtil.toJsonStr( typeList );
        //System.out.println( jsonStr );
        return jsonStr;
    }

    //返回不同配置类型页面json格式数据
    public String returnDateGridJsonStr( Integer configType , Integer page , Integer size )
    {
        Integer total = asSystemConfigDao.getTypeCount( configType );
        Integer index = ( page - 1 ) * size;
        Integer quantity = size;
        ArrayList<AsSystemConfig> list = asSystemConfigDao.getSomeType( configType , index , size );
        Pager<AsSystemConfig> pager = new Pager<>();
        pager.setTotal( total );
        pager.setRows( list );
        String jsonStr = JsonUtil.toJsonStr( pager );
        //System.out.println( jsonStr );

        return jsonStr;
    }

    //根据id查询一项
    public String returnOneById( Integer id )
    {
        AsSystemConfig one = asSystemConfigDao.selectOneById( id );

        return JsonUtil.toJsonStr( one );
    }

    //更新一项
    public String update( AsSystemConfig config )
    {
        Integer resultCode = asSystemConfigDao.updateOne( config );
        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //增加一项
    public String add( AsSystemConfig config )
    {
        //检测是否为优惠类型(优惠类型configType=7)
        if ( config.getConfigType() != 7 )
        {
            //首先获取当前类别的最大configTypeValue值
            Integer maxConfigTypeValue = asSystemConfigDao.queryMaxConfigTypeValueByConfigType( config.getConfigType() );
            //最大值上加1
            config.setConfigTypeValue( maxConfigTypeValue + 1 );
        }
        Integer resultCode = asSystemConfigDao.addOne( config );
        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //删除一项
    public String del( Integer id )
    {
        Integer resultCode = asSystemConfigDao.delOne( id );
        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //根据表中设置的最大服务年限返回服务年限combobox的json格式
    public String getServiceYearsJsonStr()
    {
        String maxServiceYearsStr = asSystemConfigDao.getMaxServiceYears();
        Integer maxServiceYears = Integer.parseInt( maxServiceYearsStr );
        JSONArray ja = new JSONArray();
        for( int i = 1 ; i <= maxServiceYears ; i++ )
        {
            JSONObject json = new JSONObject();
            json.put( "years" , i );
            ja.add( json );
        }

        return ja.toJSONString();
    }

    //返回开通app价格
    public String getOpenAppPriceJsonStr()
    {
        Double price = asSystemConfigDao.getOpenAppPrice();
        JSONObject json = new JSONObject();
        json.put( "price" , price );

        return json.toJSONString();
    }
}
